//package com.buba.config;
//
//import com.alibaba.fastjson.JSONObject;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpCookie;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpResponse;
//import org.springframework.stereotype.Component;
//import org.springframework.util.MultiValueMap;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//import utils.TokenUtil;
//
//import java.nio.charset.StandardCharsets;
//
//
//
///**
// * @author: HKY
// * @date: 2023年02月21日 19:07
// * @ClassName: AuthFilter
// * @Description: 过滤器，验证token
// * GlobalFilter 接口是用来写主要的过滤器
// * Ordered 接口是用来给全局过滤器排序，数字越大优先级越高。
// * 也可以使用注解 @Order 来给过滤器排序
// */
////@Component 实现Bean的注入
////@Component
//public class AuthFilter implements GlobalFilter, Ordered {
////    @Override
////    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
////        //跳转登入界面验证工具,如果为假就不跳转登入页面 如果跳转就跳转
////        boolean goUrlHuser = false;
////        //获取cookie
////        MultiValueMap<String, HttpCookie> cookies = exchange.getRequest().getCookies();
////        String url = exchange.getRequest().getURI().toString();
////        //如果访问的url不存在/loginController就需要鉴权
////        if (!url.contains("/loginContller/")){
//////            if (!url.contains("/loginController/")){
////            //存放token的值
////            String tokenstr = "";
////            //判断是否存在token记录
////            if (cookies.getFirst("token") == null) {
////                System.out.println("[Filter] : 不存在token");
////                goUrlHuser = true;
////            } else {
////                HttpCookie tokencookie = cookies.getFirst("token");
////                tokenstr = tokencookie.getValue();
////                System.out.println("接收到的token" + tokenstr);
////            }
////
////            //判断用户token是否过期或者恶意登入
////            if (!goUrlHuser) {
////                try {
////                    if (!TokenUtil.parseToken(tokenstr)) {
////                        System.out.println("用户可能存在恶意登入或者登入token过期");
////                        goUrlHuser = true;
////                    }
////                } catch (Exception e) {
////                    System.out.println("用户可能存在恶意登入或者登入token过期");
////                    goUrlHuser = true;
////                }
////            }
////
////            //如果没有登入或者token存在问题返回状态码和返回数据
////            if (goUrlHuser) {
////                return writeResponse(exchange.getResponse(),10002,"token验证失败");
////            }
////        }
////        return chain.filter(exchange);
////    }
////
////    @Override
////    public int getOrder() {
////        return -10;
////    }
////    /**
////     * 构建返回内容
////     *
////     * @param response ServerHttpResponse
////     * @param code     返回码
////     * @param msg     返回数据
////     */
////    protected Mono<Void> writeResponse(ServerHttpResponse response, Integer code, String msg) {
////        JSONObject message = new JSONObject();
////        message.put("code", code);
////        message.put("msg", msg);
////        byte[] bits = message.toJSONString().getBytes(StandardCharsets.UTF_8);
////        DataBuffer buffer = response.bufferFactory().wrap(bits);
////        response.setStatusCode(HttpStatus.OK);
////        // 指定编码，否则在浏览器中会中文乱码
////        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
////        return response.writeWith(Mono.just(buffer));
////    }
//
//}
